<template>
	<view class="settings-v">
		<u-cell-group>
			<!-- #ifdef APP-PLUS -->
		<!-- 	<u-cell-item title="用户协议" @click='openPage(agreement)'></u-cell-item>
			<u-cell-item title="隐私政策" @click='openPage(policy)'></u-cell-item> -->
			<!-- #endif -->
			<u-cell-item title="修改密码" @click="modifyPsd('/pages/my/modifyPsd/index')"></u-cell-item>
		<!-- 	<u-cell-item title="联系我们" @click="modifyPsd('/pages/my/contactUs/index')"></u-cell-item>
			<u-cell-item title="关于平台" @click="modifyPsd('/pages/my/abouts/index')"></u-cell-item> -->
			<u-cell-item title="版本更新" @click="updateVersion()"></u-cell-item>
		</u-cell-group>
		<view class="u-p-t-20">
			<view class="logout-cell" hover-class="u-cell-hover" @click="logout">退出登录</view>
		</view>
	</view>
</template>

<script>
	import resources from '@/libs/resources.js'
	import {
		versionUpgrade,
		getAppVersion
	} from '@/api/common.js'
	export default {
		data() {
			return {
				// #ifdef APP-PLUS
				agreement: resources.userAgreement,
				policy: resources.privacyPolicy,
				// #endif
			};
		},
		methods: {
			logout() {
				uni.showModal({
					title: '提示',
					content: '确定退出当前账号吗？',
					success: res => {
						if (res.confirm) {
							this.$store.dispatch('user/logout').then(() => {
								uni.closeSocket()
								uni.reLaunch({
									url: '/pages/login/index'
								})
							})
						}
					}
				})
			},

			updateVersion() {



				// #ifdef APP-PLUS
				let modileSystem = uni.getSystemInfoSync().platform;
				let javaApk = 'http://192.168.0.10:8017/apk/HY_MES.apk';
				let app_version = '';
				this.$nextTick(() => {
					const version = uni.getStorageSync('sysVersion')
					app_version = Number(version.replace(/[^0-9]/ig, "")) //version;

					if (app_version == 0) {
						getAppVersion().then(res => {
							let data = res.data.sysVersion || ''
							uni.setStorageSync('sysVersion', data)
							app_version = Number(data.replace(/[^0-9]/ig, ""))
						}).catch((err) => {})
					}
				})
				plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
					versionUpgrade().then(res => {
						let resVersion = res.data.AppVersion || '';
						resVersion = Number(resVersion.replace(/[^0-9]/ig, ""))

					//	if (app_version < resVersion) {
							process.env.NODE_ENV === "production" ?
								uni.setStorageSync('isUpdate', 1) : uni.removeStorageSync('isUpdate')
							uni.showModal({ //提醒用户更新
								title: "系统提示",
								content:"立即更新版本",
								success: (res) => {
									if (res.confirm) {
										uni.removeStorageSync('isUpdate')
										let system = plus.os.name;
										if (system === 'Android') {
											let url = javaApk;
											plus.runtime.openURL(url)
											uni.downloadFile({
												//下载地址
												url: url,
												success: data => {
													console.log(data)
													if (data.statusCode === 200) {
														plus.runtime.install(data
															.tempFilePath, {
																force: false
															},
															function() {
																plus.runtime
																	.restart();
															});
													}
												}
											})
										} else {
											let appleId = devLanguage ? 1490797314 :
												1563667378;
											plus.runtime.launchApplication({
												action: `itms-apps://itunes.apple.com/cn/app/id${appleId}`
											}, function(e) {});
										}
									} else if (res.cancel) {
										if (modileSystem == 'ios') {
											plus.ios.import("UIApplication")
												.sharedApplication()
												.performSelector("exit")
										} else if (modileSystem == 'android') {
											//plus.runtime.quit();
											//close()
										}
									}
								}
							})
						//}
					})
				})

				// #endif





			},

			modifyPsd(path) {
				if (!path) return
				uni.navigateTo({
					url: path
				})
			},
			// #ifdef APP-PLUS
			openPage(url) {
				plus.runtime.openURL(url);
			}
			// #endif
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f0f2f6;
	}

	.settings-v {
		.logout-cell {
			text-align: center;
			font-size: 28rpx;
			line-height: 100rpx;
			background-color: #fff;
		}
	}
</style>